import { createRouter, createWebHashHistory,createWebHistory } from 'vue-router'
export const Layout = () => import('@/layout/index.vue');
import store from '@/store/index.js'
// import getIndexPath from './getIndexPath.js'
const router=createRouter({
  // history:createWebHashHistory(),
  history:createWebHistory(),
  routes:[
    {
      path: '/',
      component: Layout,
      redirect: '/dashboard',
      // redirect:store.state.default_url,
      children: [
        {
          path:'/dashboard',
          name:'dashboard',
          component:()=>import("@/views/dashboard.vue")
        },
        {
           path:'/adminer',
           name:'adminer',
           component:()=>import("@/views/adminer.vue")
        },
        {
           path:'/userAdmin',
           name:'userAdmin',
           component:()=>import("@/views/userAdmin.vue")
        },
        // {
        //    path:'/activity',
        //    name:'activity',
        //    component:()=>import("@/views/activity.vue")
        // },
        {
           path:'/order',
           name:'order',
           component:()=>import("@/views/order.vue")
        },
        // {
        //    path:'/service',
        //    name:'service',
        //    component:()=>import("@/views/service.vue")
        // },
        // { // 卡券
        //    path:'/coupon',
        //    name:'coupon',
        //    component:()=>import("@/views/coupon.vue")
        // },
        {// 门店管理
           path:'/store/store',
           name:'store',
           component:()=>import("@/views/store/store.vue")
        },
        { // 门店卡券
           path:'/store/coupon',
           name:'store_coupon',
           component:()=>import("@/views/store/coupon.vue")
        },{ // 门店团购
           path:'/store/purchase',
           name:'store_purchase',
           component:()=>import("@/views/store/purchase.vue")
        },
        {
           path:'/store/activity',
           name:'activity',
           component:()=>import("@/views/activity.vue")
        },
        {
           path:'/store/service',
           name:'service',
           component:()=>import("@/views/service.vue")
        },
        {
           path:'/scale',
           name:'scale',
           component:()=>import("@/views/scale.vue")
        },
      ]
    },
    {
      path:"/login",
      name:'login',
      component:()=>import("@/views/login.vue")
    },
    {
      path:"/register",
      name:'register',
      component:()=>import("@/views/register.vue")
    },
  ]
})

router.beforeEach(async (to, from) => {
  const loginState = await store.dispatch('checkLogin',{type:'get'})
  // console.log(loginState,to.name)
  if(loginState == -1 && to.name != 'login' && to.name != 'register'){
    return { name: 'login' }
  }
  // 判断是否登录
  // console.log('路由导航:');
  // console.log(store.state.token);
  // store.state.token;
  // if(!store.state.token){
  //   return { name: 'login' }
  // }
  // var hasLogin = localStorage.getItem('hasLogin');
  // if (hasLogin != 1 && to.name != 'login'){
  //   return { name: 'login' }
  // }
})
export default router
